<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div slot="header">
          <el-row type="flex" justify="space-between" align="middle">
            <span>权限管理</span>
            <el-button type="primary" @click="dakaitanchuang(1, '0')">
              添加权限
            </el-button>
          </el-row>
        </div>
        <!-- card body -->
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="类型" prop="type" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-if="row.type === 1"
                type="text"
                @click="dakaitanchuang(2, row.id)"
              >
                添加
              </el-button>
              <el-button type="text" @click="bianji(row.id)">编辑</el-button>
              <el-button type="text" @click="xdelPermission(row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!--      弹出层        -->
    <el-dialog :visible="showDialog" title="添加权限" @close="ashowDialog">
      <el-form ref="formData" label-width="100px" :model="formData">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="add">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// 导入API : 获取权限列表 添加权限管理 删除 更新
import { getPermissionList, addPermission, delPermission, updatePermission, getPermissionDetail } from '@/api/permission.js'
// 导入转换树形结构
import { tranListToTreeData } from '@/utils/index.js'

export default {
  name: 'Permission',
  data () {
    return {
      list: [], // 权限列表
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限点名称不能为空', trigger: 'blur' }]
      }

    }
  },
  created () {
    this.xgetPermissionList()
  },
  methods: {
    // 获取权限列表数据
    async xgetPermissionList () {
      //  调用接口获取权限列表
      const res = await getPermissionList()
      console.log(res)
      console.table(res)
      // 转换树形结构
      this.list = tranListToTreeData(res)
    },
    // 打开弹窗
    dakaitanchuang (type, id) {
      this.showDialog = true
      // 记录当前添加添加点的标识
      this.formData.type = type
      // 添加到那个节点下
      this.formData.pid = id
    },
    // 关闭清空
    ashowDialog () {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
      // 清空校验
      this.$refs.formData.resetFields()
    },
    // 添加 和 更新
    add () {
      // 校验表单
      this.$refs.formData.validate(async (isOK) => {
        // 判断校验是否成功
        if (!isOK) return
        // 是否有 id
        if (this.formData.id) {
          // 有 就是编辑
          await updatePermission(this.formData)
        } else {
          // 没有 就是添加
          await addPermission(this.formData)
        }
        this.$message.success('添加成功！')
        this.xgetPermissionList()
        this.showDialog = false
      })
    },
    // 删除
    xdelPermission (row) {
      this.$confirm('确定要删除吗？', '提示').then(async () => {
        if (row.children) {
          this.$message.success('不能直接删除父节点！')
        } else {
          await delPermission(row.id)
          this.xgetPermissionList()
          this.$message.success('删除成功！')
        }
      }).catch(console.log)
    },
    // 数据回填
    async bianji (id) {
      // 获取数据
      const res = await getPermissionDetail(id)
      //  进行回填
      this.formData = res
      //  显示弹出层
      this.showDialog = true
    }

  }
}
</script>

<style lang="scss" scoped>
</style>
